﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;

public partial class TaoBaoApi_TB_ShangDian_Edit : System.Web.UI.Page
{
    /// <summary>
    /// 加载页面主题模版
    /// </summary>  
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    /// <summary>
    /// 初始化加载数据，并判断当前操作为添加或更新
    /// </summary>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindOrderFrom();
            BindPayType();
            if (Code == null)
            {
                lblShop.Text = "添加淘宝商店";
                IsAdd = true;
                btnSave.Text = "添加";
            }
            else
            {
                lblShop.Text = "更新淘宝商店";
                Property = new BLL.TaoBao.TB_SHANGDIAN().GetModel(int.Parse(Code));
                if (Property != null)
                {
                    txtZH.Text = Property.TBZH;
                    txtNC.Text = Property.TBNC;
                    txtPsd.Text = Property.PWD;
                    ddlLY.Text = Property.DDLY;
                    txtZFB.Text = Property.field1;
                    drpType.SelectedValue = Property.field2;
                    IsAdd = false;
                    btnSave.Text = "更新";
                }
                else
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('系统中无此记录!');location.href='tb_shangdian.aspx';</script>");
            }
        }
    }
    /// <summary>
    /// 接受页面传入参数
    /// </summary>
    private string Code
    {
        get { 
            string code = Request.QueryString["code"];
            if (code == null || code.Length == 0)
                return null;
            return code;
        }
    }
    /// <summary>
    /// 判断是否添加，否则为编辑
    /// </summary>
    private bool IsAdd
    {
        get { return Convert.ToBoolean(ViewState["IsAdd"]); }
        set { ViewState["IsAdd"] = value; }
    }
    /// <summary>
    /// 淘宝商店属性
    /// </summary>
    private Model.TaoBao.TB_SHANGDIAN Property
    {
        get
        {
            Model.TaoBao.TB_SHANGDIAN property = null;
            if (ViewState["Property"] != null)
                property = (Model.TaoBao.TB_SHANGDIAN)ViewState["Property"];
            return property;
        }
        set
        {
            ViewState["Property"] = value;
        }
    }
    /// <summary>
    /// 绑定订单来源
    /// </summary>
    void BindOrderFrom()
    {
        List<Model.ORDERFROM> orders = new BLL.ORDERFROM().GetAllList();        
        ddlLY.DataSource = orders;
        ddlLY.DataTextField = "OF_NAme";
        ddlLY.DataValueField = "OF_Code";
        ddlLY.DataBind();
    }
    /// <summary>
    /// 绑定支付方式
    /// </summary>
    void BindPayType()
    {
        List<Model.PAYTYPE> types = new BLL.PAYTYPE().GetAllList();
        drpType.DataSource = types;
        drpType.DataTextField = "PT_NAME";
        drpType.DataValueField = "PT_CODE";
        drpType.DataBind();
    }
    /// <summary>
    /// 由来源代码获取来源名称
    /// </summary>
    /// <param name="orderLYCode"></param>
    /// <returns></returns>
    string GetOrderLYName(string orderLYCode)
    {
        Model.ORDERFROM order = new BLL.ORDERFROM().GetModel(orderLYCode);
        return order.OF_NAME;
    }
    /// <summary>
    /// 取消当前操作
    /// </summary>  
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("tb_shangdian.aspx");
    }
    /// <summary>
    /// 保存数据
    /// </summary>   
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!ValidInput())
            return;
        string zh = txtZH.Text.TrimEnd();
        string nc = txtNC.Text.TrimEnd();
        string psd = txtPsd .Text.TrimEnd();
        string lyCode = ddlLY.Text.TrimEnd();
        string note = txtNote.Text;
        if (IsAdd)
        {
            Model.TaoBao.TB_SHANGDIAN tb = new Model.TaoBao.TB_SHANGDIAN();
            tb.TBZH = zh;
            tb.TBNC = nc;
            tb.PWD = psd;
            tb.DDLY = lyCode;
            tb.BZ = note;
            tb.TN_ID = 0;
            tb.field1 = txtZFB.Text;
            tb.field2 = drpType.SelectedValue;
            if (new BLL.TaoBao.TB_SHANGDIAN().Add(tb) > 0)
                ShowMessage("添加淘宝商店成功。");
            else
                ShowMessage("添加淘宝商店失败，错误代码：600012。");
        }
        else
        {
            int mid = Convert.ToInt32(Code);
            Property.MID = mid;
            Property.TBZH = zh;
            Property.TBNC = nc;
            Property.PWD = psd;
            Property.DDLY = lyCode;
            Property.BZ = note;
            Property.field1 = txtZFB.Text;
            Property.field2 = drpType.SelectedValue;
            if (new BLL.TaoBao.TB_SHANGDIAN().Update(Property))
                ShowMessage("更新淘宝商店成功。");
            else
                ShowMessage("更新淘宝商店失败，错误代码：700012。");
        }
    }
    /// <summary>
    /// 验证输入
    /// </summary>
    bool ValidInput()
    {
        if (txtZH.Text.TrimEnd().Length == 0 || txtNC.Text.TrimEnd().Length == 0 || txtPsd.Text.TrimEnd().Length == 0)
        {
            ShowMessage("淘宝账户、昵称、密码都不允许为空。");
            return false;
        }
        if (ddlLY.Text.Length == 0)
        {
            ShowMessage("请选择来源。");
            return false;
        }
        return true;
    }   
    /// <summary>
    /// 显示提示信息
    /// </summary>
    void ShowMessage(string msg)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('"+msg+"')", true);
    }    
}
